package com.lebang.configuration;


import com.lebang.entity.LbbAppStore;
import com.lebang.mapper.LbbAppStoreMapper;
import com.lebangweb.common.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.Date;
import java.util.List;

/**
 *
 * @Description 将version版本号写入application中，给css,js引用时用
 * @author Eliohn on 2018/10/1.
 */
@Component
public class ApplicationContext implements ServletContextAware {


    @Value("${info.version}")
    private String version;
    @Value("${info.name}")
    private String appname;
    @Autowired
    private LbbAppStoreMapper appStoreMapper;

    private static final Logger log = LoggerFactory.getLogger(ApplicationContext.class);

    /**
     *
     * <p>初始化到Application作用域当中</p>
     * @param context
     * @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
     */
    @Override
    public void setServletContext(ServletContext context) {
        List<LbbAppStore> list=appStoreMapper.selectBrand();
        String datetime = DateUtil.TimeFormat.LONG_DATE_PATTERN_WITH_MILSEC_NO.format(new Date());
        String contextPath = context.getContextPath();
        log.info("# version={} , contextPath={}", datetime, contextPath);
        context.setAttribute("appStoreList", list);
        context.setAttribute("version_css", datetime);
        context.setAttribute("version_js", datetime);
        context.setAttribute("ctx", "");
        context.setAttribute("pver", version);
        context.setAttribute("appname", appname);
    }

}
